from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.spu_serializers import *
from meiduo_admin.paginations import MyPage
from rest_framework.generics import ListAPIView

# 当前视图可以用来实现序列化多条分类信息
# 无非就是序列化的查询集不一样
#获取一级二级
class SPUCateView(ListAPIView):
    queryset = GoodsCategory.objects.all()# 默认返回所有分类
    serializer_class = CateSimpleSerializer

    def get_queryset(self):
        parent_id = self.kwargs.get('pk')
        # 父级分类id
        # 如果此次接口请求路由有pk分组参数 —— 返回根据pk过滤出来的二级或三级分类查询集

        if parent_id:
            return self.queryset.filter(parent_id=parent_id)
        #如果接口请求路径没有pk,返回一级分类
        return self.queryset.filter(parent=None)

# 获取可选品牌
class SPUBrandView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSimpleSerializer


#新建，更新，删除spu
class SPUGoodsView(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(
                name__contanins = keyword
            )
        return self.queryset.all()
